package lzzy.edu.cn.demothymeleaf.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@Entity(name = "t_customer")
@Table(name = "t_customer")
public class Customer implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "username", unique = true, nullable = false)
    @Length(min = 3, message = "用户账号至少包含3个字符")
    @NotEmpty(message = "用户账号为必填字段")
    private String username;

    @Column(name = "password")
    @Length(min = 5, message = "密码至少五个字符")
    @NotEmpty(message = "密码为必填字段")
    private String password;

    private Integer valid;

//    @Column(name = "email", unique = true, nullable = false)
//    @NotEmpty(message = "请提供有效的Email地址")
    private String email;

    private String phone;

    private String qq;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id")
    private Authority authority;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (this.authority != null) {
            authorities.add(new SimpleGrantedAuthority(this.authority.getAuthority()));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
